|
1
|
|
|
/* |
|
2
|
|
|
* Copyright (c) 2018 Rafael da Silva Rocha. |
|
3
|
|
|
* |
|
4
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining |
|
5
|
|
|
* a copy of this software and associated documentation files (the |
|
6
|
|
|
* "Software"), to deal in the Software without restriction, including |
|
7
|
|
|
* without limitation the rights to use, copy, modify, merge, publish, |
|
8
|
|
|
* distribute, sublicense, and/or sell copies of the Software, and to |
|
9
|
|
|
* permit persons to whom the Software is furnished to do so, subject to |
|
10
|
|
|
* the following conditions: |
|
11
|
|
|
* |
|
12
|
|
|
* The above copyright notice and this permission notice shall be |
|
13
|
|
|
* included in all copies or substantial portions of the Software. |
|
14
|
|
|
* |
|
15
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, |
|
16
|
|
|
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF |
|
17
|
|
|
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND |
|
18
|
|
|
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE |
|
19
|
|
|
* LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION |
|
20
|
|
|
* OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION |
|
21
|
|
|
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
|
22
|
|
|
* |
|
23
|
|
|
*/ |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* @fileoverview Functions to read and write numbers and strings as bytes. |
|
27
|
|
|
* @see https://github.com/rochars/minibuffer |
|
28
|
|
|
*/ |
|
29
|
|
|
|
|
30
|
|
|
/** @module minibuffer */ |
|
31
|
|
|
|
|
32
|
|
|
import * as byteData from 'byte-data'; |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* Read a string of UTF-8 characters from a byte buffer. |
|
36
|
|
|
* @param {!Uint8Array|!Array<number>} buffer A byte buffer. |
|
37
|
|
|
* @param {number=} index The buffer index to start reading. |
|
38
|
|
|
* @param {number=} end The buffer index to stop reading, non inclusive. |
|
39
|
|
|
* Assumes buffer length if undefined. |
|
40
|
|
|
* @return {string} |
|
41
|
|
|
*/ |
|
42
|
|
|
export function unpackString(buffer, index=0, end=buffer.length) { |
|
43
|
|
|
return byteData.unpackString(buffer, index, end); |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
|
|
* Write a string of UTF-8 characters as a byte buffer. |
|
48
|
|
|
* @param {string} str The string to pack. |
|
49
|
|
|
* @return {!Array<number>} The UTF-8 string bytes. |
|
50
|
|
|
*/ |
|
51
|
|
|
export function packString(str) { |
|
52
|
|
|
return byteData.packString(str); |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
/** |
|
56
|
|
|
* Write a string of UTF-8 characters to a byte buffer. |
|
57
|
|
|
* @param {string} str The string to pack. |
|
58
|
|
|
* @param {!Uint8Array|!Array<number>} buffer The output buffer. |
|
59
|
|
|
* @param {number=} index The buffer index to start writing. |
|
60
|
|
|
* Assumes zero if undefined. |
|
61
|
|
|
* @return {number} The next index to write in the buffer. |
|
62
|
|
|
*/ |
|
63
|
|
|
export function packStringTo(str, buffer, index=0) { |
|
64
|
|
|
return byteData.packStringTo(str, buffer, index); |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
// Numbers |
|
68
|
|
|
/** |
|
69
|
|
|
* Pack a number as a byte buffer. |
|
70
|
|
|
* @param {number} value The number. |
|
71
|
|
|
* @param {!Object} theType The type definition. |
|
72
|
|
|
* @return {!Array<number>} The packed value. |
|
73
|
|
|
* @throws {Error} If the type definition is not valid. |
|
74
|
|
|
* @throws {Error} If the value is not valid. |
|
75
|
|
|
*/ |
|
76
|
|
|
export function pack(value, theType) { |
|
77
|
|
|
return byteData.pack(value, theType); |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
/** |
|
81
|
|
|
* Pack a number to a byte buffer. |
|
82
|
|
|
* @param {number} value The value. |
|
83
|
|
|
* @param {!Object} theType The type definition. |
|
84
|
|
|
* @param {!Uint8Array|!Array<number>} buffer The output buffer. |
|
85
|
|
|
* @param {number=} index The buffer index to write. Assumes 0 if undefined. |
|
86
|
|
|
* @return {number} The next index to write. |
|
87
|
|
|
* @throws {Error} If the type definition is not valid. |
|
88
|
|
|
* @throws {Error} If the value is not valid. |
|
89
|
|
|
*/ |
|
90
|
|
|
export function packTo(value, theType, buffer, index=0) { |
|
91
|
|
|
return byteData.packTo(value, theType, buffer, index); |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
/** |
|
95
|
|
|
* Pack an array of numbers as a byte buffer. |
|
96
|
|
|
* @param {!Array<number>|!TypedArray} values The values. |
|
97
|
|
|
* @param {!Object} theType The type definition. |
|
98
|
|
|
* @return {!Array<number>} The packed values. |
|
99
|
|
|
* @throws {Error} If the type definition is not valid. |
|
100
|
|
|
* @throws {Error} If any of the values are not valid. |
|
101
|
|
|
*/ |
|
102
|
|
|
export function packArray(values, theType) { |
|
103
|
|
|
return byteData.packArray(values, theType); |
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
|
|
/** |
|
107
|
|
|
* Pack a array of numbers to a byte buffer. |
|
108
|
|
|
* @param {!Array<number>|!TypedArray} values The value. |
|
109
|
|
|
* @param {!Object} theType The type definition. |
|
110
|
|
|
* @param {!Uint8Array|!Array<number>} buffer The output buffer. |
|
111
|
|
|
* @param {number=} index The buffer index to start writing. |
|
112
|
|
|
* Assumes zero if undefined. |
|
113
|
|
|
* @return {number} The next index to write. |
|
114
|
|
|
* @throws {Error} If the type definition is not valid. |
|
115
|
|
|
* @throws {Error} If the value is not valid. |
|
116
|
|
|
*/ |
|
117
|
|
|
export function packArrayTo(values, theType, buffer, index=0) { |
|
118
|
|
|
return byteData.packArrayTo(values, theType, buffer, index); |
|
119
|
|
|
} |
|
120
|
|
|
|
|
121
|
|
|
/** |
|
122
|
|
|
* Unpack a number from a byte buffer. |
|
123
|
|
|
* @param {!Uint8Array|!Array<number>} buffer The byte buffer. |
|
124
|
|
|
* @param {!Object} theType The type definition. |
|
125
|
|
|
* @param {number=} index The buffer index to read. Assumes zero if undefined. |
|
126
|
|
|
* @return {number} |
|
127
|
|
|
* @throws {Error} If the type definition is not valid |
|
128
|
|
|
* @throws {Error} On bad buffer length. |
|
129
|
|
|
* @throws {Error} On overflow |
|
130
|
|
|
*/ |
|
131
|
|
|
export function unpack(buffer, theType, index=0) { |
|
132
|
|
|
return byteData.unpack(buffer, theType, index); |
|
133
|
|
|
} |
|
134
|
|
|
|
|
135
|
|
|
/** |
|
136
|
|
|
* Unpack an array of numbers from a byte buffer. |
|
137
|
|
|
* @param {!Uint8Array|!Array<number>} buffer The byte buffer. |
|
138
|
|
|
* @param {!Object} theType The type definition. |
|
139
|
|
|
* @param {number=} start The buffer index to start reading. |
|
140
|
|
|
* Assumes zero if undefined. |
|
141
|
|
|
* @param {number=} end The buffer index to stop reading. |
|
142
|
|
|
* Assumes the buffer length if undefined. |
|
143
|
|
|
* @param {boolean=} safe If set to false, extra bytes in the end of |
|
144
|
|
|
* the array are ignored and input buffers with insufficient bytes will |
|
145
|
|
|
* output a empty array. If safe is set to true the function |
|
146
|
|
|
* will throw a 'Bad buffer length' error. Defaults to false. |
|
147
|
|
|
* @return {!Array<number>} |
|
148
|
|
|
* @throws {Error} If the type definition is not valid |
|
149
|
|
|
* @throws {Error} On overflow |
|
150
|
|
|
*/ |
|
151
|
|
|
export function unpackArray( |
|
152
|
|
|
buffer, theType, start=0, end=buffer.length, safe=false) { |
|
153
|
|
|
return byteData.unpackArray(buffer, theType, start, end, safe); |
|
154
|
|
|
} |
|
155
|
|
|
|
|
156
|
|
|
/** |
|
157
|
|
|
* Unpack a array of numbers to a typed array. |
|
158
|
|
|
* @param {!Uint8Array|!Array<number>} buffer The byte buffer. |
|
159
|
|
|
* @param {!Object} theType The type definition. |
|
160
|
|
|
* @param {!TypedArray|!Array<number>} output The output array. |
|
161
|
|
|
* @param {number=} start The buffer index to start reading. |
|
162
|
|
|
* Assumes zero if undefined. |
|
163
|
|
|
* @param {number=} end The buffer index to stop reading. |
|
164
|
|
|
* Assumes the buffer length if undefined. |
|
165
|
|
|
* @param {boolean=} safe If set to false, extra bytes in the end of |
|
166
|
|
|
* the array are ignored and input buffers with insufficient bytes will |
|
167
|
|
|
* write nothing to the output array. If safe is set to true the function |
|
168
|
|
|
* will throw a 'Bad buffer length' error. Defaults to false. |
|
169
|
|
|
* @throws {Error} If the type definition is not valid |
|
170
|
|
|
* @throws {Error} On overflow |
|
171
|
|
|
*/ |
|
172
|
|
|
export function unpackArrayTo( |
|
173
|
|
|
buffer, theType, output, start=0, end=buffer.length, safe=false) { |
|
174
|
|
|
byteData.unpackArrayTo(buffer, theType, output, start, end, safe); |
|
175
|
|
|
} |
|
176
|
|
|
|